﻿using AssociationManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AssociationManagerTests
{
    public partial class MainForm : Form
    {
        private static string[] TestExtensions = { ".txt", ".sln", ".cs", ".nuspec", ".zip", ".exe", ".accdb", ".unknown", ".pcapng", ".html" };

        private FileAssociationManager _currentUser = new FileAssociationManager(FileAssociationManager.AssociationLocation.CurrentUser);
        private FileAssociationManager _localMachine = new FileAssociationManager(FileAssociationManager.AssociationLocation.LocalMachine);

        public MainForm()
        {
            InitializeComponent();

            listViewExtensions.BeginUpdate();
            try
            {
                foreach (string extension in TestExtensions)
                {
                    try
                    {
                        using (ApplicationAssociation fileExt = GetAssociation(extension))
                        {
                            ApplicationIcon appIcon = fileExt.DefaultIcon;

                            ListViewItem item = new ListViewItem();
                            item.Text = fileExt.Extension;
                            item.SubItems.Add(fileExt.ShellOpenCommand);
                            item.SubItems.Add(fileExt.Description);
                            item.SubItems.Add(appIcon?.IconLibraryPath);
                            item.SubItems.Add(appIcon?.IconIndex.ToString());
                            item.SubItems.Add(fileExt.EditFlags.ToString());
                            item.SubItems.Add(fileExt.InfoTip);

                            try
                            {
                                if (appIcon != null)
                                {
                                    Icon icon = appIcon.GetIcon(IconManager.IconSize.Small, extension);
                                    // IconManager iconMgr = new IconManager(appIcon);
                                    // Icon icon = iconMgr.GetIcon(IconManager.IconSize.Small, "*" + extension);

                                    imageListIcons.Images.Add(icon);
                                    item.ImageIndex = imageListIcons.Images.Count - 1;
                                }
                            }
                            catch { }

                            listViewExtensions.Items.Add(item);
                        }
                    }
                    catch
                    {
                        ListViewItem item = new ListViewItem();
                        item.Text = extension;
                        item.SubItems.Add("<Unknown Extension>");

                        listViewExtensions.Items.Add(item);
                    }
                }
            }
            finally
            {
                listViewExtensions.EndUpdate();
            }
        }

        private ApplicationAssociation GetAssociation(string extension)
        {
            try
            {
                return _currentUser.GetFileAssociation(extension);
            }
            catch
            {
                return _localMachine.GetFileAssociation(extension);
            }
        }
    }
}